package yy;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Account {
	private int balance;
	private Lock lock = new ReentrantLock();
	private Condition plus  = lock.newCondition(); 
	public Account(int balance) {
		super();
		this.balance = balance;
	}

	public int getBalance() {// synchronized
		return balance;
	}

	public void deposit(int money) {// synchronized
		lock.lock();
		try {
			balance += money;
			plus.signalAll();
		} finally {
			lock.unlock();
		}
	}

	public void withdraw(int money) {// synchronized
		lock.lock();
		try {
			while (getBalance() <= 0) {
				try{
				plus.await();
				}catch(InterruptedException e){
					throw new RuntimeException(e);
				}
			}
			balance -= money;
		} finally {
			lock.unlock();
		}
	}
}
